package com.ivt.controller.teacher;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.ivt.core.util.Constants;
import com.ivt.model.Attendance;
import com.ivt.model.Courses;
import com.ivt.model.Teacher;
import com.ivt.model.view.AttendanceView;
import com.ivt.model.view.QueryCriteria;
import com.ivt.model.view.Rst;
import com.ivt.service.AttendanceService;
import com.ivt.service.CoursesService;
import com.ivt.utils.Convert;

@Controller
@RequestMapping("/teacher/attendance")
public class AttendanceController {

	@Resource
	private AttendanceService attendanceService;

	@Resource
	private CoursesService coursesService;

	@ModelAttribute
	public void attr(ModelMap view) {
		view.addAttribute("moduleName", "attendance");
		view.addAttribute("moduleTitle", "签到信息");
	}

	@RequestMapping("/create")
	public String create(ModelMap view) {
		view.addAttribute("moduleAction", "save");
		return "alone:teacher.attendance-create";
	}

	@RequestMapping("/list")
	public String list(ModelMap view, final HttpServletRequest request) {

		HttpSession session = request.getSession();

		Teacher teacher = (Teacher) session
				.getAttribute(Constants.SESSION_TEACHER_KEY);
		view.addAttribute("teacher", teacher);
		List<Courses> courses = coursesService.getCoursesByTeacher(teacher);
		view.addAttribute("courses", courses);
		return "alone:teacher.attendance-list";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap view) {

		Attendance attendance = attendanceService.getAttendance(id);

		view.addAttribute("row", attendance);
		view.addAttribute("moduleAction", "update");
		return "alone:teacher.attendance-modify";
	}

	@RequestMapping("/save")
	@ResponseBody
	public Rst save(Attendance attendance) {

		Rst rst = new Rst();
		attendance = attendanceService.create(attendance);
		if (0 < attendance.getAttendanceId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/pagination")
	@ResponseBody
	public Rst pagination(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		List<Attendance> list = attendanceService
				.queryClassesByCriteria(queryCriteria);

		List<AttendanceView> views = Lists.newArrayList();

		if (CollectionUtils.isNotEmpty(list)) {
			for (Attendance attendance : list) {
				views.add(Convert.toAttendanceView(attendance));
			}
		}

		rst.setCode(Constants.SUCCESS);
		rst.setData(views);
		return rst;
	}

	@RequestMapping("/pages")
	@ResponseBody
	public Rst pages(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		long count = attendanceService.queryCount(queryCriteria);
		rst.setCode(Constants.SUCCESS);
		rst.setData(count);
		return rst;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Rst update(Attendance attendance) {
		Rst rst = new Rst();
		attendance = attendanceService.update(attendance);
		if (0 < attendance.getAttendanceId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Rst update(Long[] ids) {
		Rst rst = new Rst();
		int c = attendanceService.deletes(ids);
		rst.setCode(Constants.SUCCESS);
		rst.setMessage("操作成功");
		rst.setData(c);
		return rst;
	}
}
